#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e5 + 10;
const int B = 310;
const int T = N / B + 10;

int n, m, tr[N], bel[N], xl[N], xr[N], a[N], b[N];
bool vis[N];
ll f[T][N], bf[T][T], pre[N], suf[N];

void add(int x, int v) { for(; x <= n; x += x & -x) tr[x] += v; }
int query(int x) { int res = 0; for(; x; x -= x & -x) res += tr[x]; return res; }
void reset() { memset(tr, 0, sizeof tr); }

int x[N], y[N], tx, ty;

int calc() {
	ll res = 0;
	int p = 1;
	rep(i, 1, ty) {
		while(p <= tx && x[p] <= y[i]) p++;
		res += tx - (p - 1);
	} return res;
}

ll query(int l, int r) {
	reset(); ll res = 0;
	rep(i, l, r) {
		res += query(n) - query(a[i]);
		add(a[i], 1);
	} return res;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin); freopen("P5046_bf.out", "w", stdout);
#endif	
	n = in, m = in;
	rep(i, 1, n) a[i] = in, b[i] = i, bel[i] = (i - 1) / B + 1, xr[bel[i]] = i;
	rep(i, 1, bel[n]) {
		xl[i] = xr[i - 1] + 1;
		sort(b + xl[i], b + xr[i] + 1, [](auto x, auto y) { return a[x] < a[y]; });
		reset();
		rep(j, xl[i], xr[i]) {
			if(j == xl[i]) pre[j] = 0; else pre[j] = pre[j - 1];
			pre[j] += query(n) - query(a[j]); add(a[j], 1);
		} reset();
		per(j, xr[i], xl[i]) {
			if(j == xr[i]) suf[j] = 0; else suf[j] = suf[j + 1];
			suf[j] += query(a[j]); add(a[j], 1);
		} bf[i][i] = suf[xl[i]];
	}
	rep(i, 1, bel[n]) {
		reset();
		rep(j, xl[i], xr[i]) add(a[j], 1);
		rep(j, 1, xl[i] - 1) f[i][j] = query(a[j]);
		rep(j, xl[i], xr[i]) f[i][j] = query(a[j] - 1), add(a[j], -1);
		rep(j, xl[i], xr[i]) add(a[j], 1);
		rep(j, xr[i] + 1, n) f[i][j] = query(n) - query(a[j]);
		rep(j, 1, n) f[i][j] += f[i][j - 1];
	}
	rep(i, 1, bel[n]) rep(j, i + 1, bel[n]) bf[i][j] = bf[i + 1][j] + bf[i][j - 1] - bf[i - 1][j - 1] + f[i][xr[j]] - f[i][xl[j] - 1];
	ll ans = 0; reset(); 
	while(m--) {
		ll l = in, r = in;
		printf("%lld\n", ans = query(l, r));
	}
	return 0;
}
